define('youa.module.sn.share', [
  'zepto',
  'zepto.cookie',
  'http://res.wx.qq.com/open/js/jweixin-1.1.0.js',
  'http://a.tepin.com/activity/iface/youayun-2.0.js'
], function ($, $cookie, wx) {
  'use strict';

  window.wx = wx;

  /**
   * 获取请求地址中的参数
   */
  function getParam() {
    var param = window.location.search;
    if (param.indexOf("tag=") > 0) {
      param.split("&");
      var tag_value = param.substring(param.indexOf("tag="));
      if (tag_value.indexOf("&") > 0) {
        tag_value = tag_value.substring(4, tag_value.indexOf("&"));
      } else {
        tag_value = tag_value.substring(4);
      }
      if (tag_value) {//是否有标示
        //存cookie
        try {
          $.fn.cookie('hwg_tag', tag_value, { path: "/", domain: 'tepin.hk' });
        } catch (e) {
          console.log("cookie save tag js is error ");
        }
        var _href = window.location.href;
        $.ajax({
          type: "POST",
          url: "/shop/setTag.html",
          data: {
            'tag': tag_value,
            'path': _href
          },
          success: function () {

          }
        });
      }
    }
  };

  getParam()

  var map = [{
    url: 'http://m.tepin.hk/activity/15.html',
    desc: '我在友阿海外购挑选轻奢商品，一起来看看吧。'
  }, {
    url: 'http://m.tepin.hk/activity/29.html',
    desc: '我在友阿海外购挑选保健商品，一起来看看吧。'
  }, {
    url: 'http://m.tepin.hk/activity/18.html',
    desc: '我在友阿海外购挑选家居商品，一起来看看吧。'
  }, {
    url: 'http://m.tepin.hk/activity/36.html',
    desc: '我在友阿海外购挑选美妆商品，一起来看看吧。'
  }, {
    url: 'http://m.tepin.hk/activity/14.html',
    desc: '我在友阿海外购挑选母婴商品，一起来看看吧。'
  }]

  var runner = window.youayun({
    clientId: "haiwaigou",
    channel: "wap",
    skin: "haiwaigou-wap"
  });

  //设置微信参数
  function shareToWx() {
    $.ajax({
      type: "POST",
      url: "/shop/membercenter/getMember.html",
      dataType: "json",
      success: function (data) {
        var link;

        if (data.id) {
          var _hwg_href = window.location.href;
          if (_hwg_href.indexOf("?") > 0) {
            if (_hwg_href.indexOf('tag') > 0) {
              var _head = _hwg_href.substring(0, _hwg_href.indexOf("tag="));
              var _param = _hwg_href.substring(_hwg_href.indexOf("tag="));
              if (_param.indexOf("&") > 0) {
                _param = _param.substring(_param.indexOf("&"));
                _param = "tag=" + data.mobileNumber + _param;
              } else {
                _param = "tag=" + data.mobileNumber;
              }
              link = _head + _param;
            } else {
              link = window.location.href + '&tag=' + data.mobileNumber;
            }
          } else {
            link = window.location.href + '?tag=' + data.mobileNumber;
          }

        } else {
          link = window.location.href;
        }

        var desc = "我在友阿海外购挑选商品，一起来看看吧。"
        var isTarget = false;
        for (var i = 0; i < map.length; i++) {
          if (window.location.href.indexOf(map[i].url) > -1) {
            desc = map[i].desc;
            isTarget = true;
          }
        }

        if (!isTarget && window.location.href.indexOf('http://m.tepin.hk/activity') > -1) {
          desc = $('meta[name="description"]').attr('content')
        }

        var title = $('title').text(),
          appId = "wxa0e9e4df789628ba",
          desc = desc,
          hideMenuItem = 0,
          jsApList = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQZone', 'onMenuShareQQ', 'onMenuShareWeibo', 'hideMenuItems', 'addCard'],
          pageUrl = window.location,
          imgUrl = $('meta[name="shareicon"]').attr('content') || "http://a5.mzstatic.com/us/r30/Purple71/v4/56/df/50/56df5058-8a1e-ac58-1af5-24c2e4817d15/icon175x175.png";
        
        var option = {
          'appId': appId,
          'jsApList': jsApList,
          'hideMenuItem': hideMenuItem,
          'title': title,
          'desc': desc,
          'link': link,
          'imgUrl': imgUrl,
          'pageUrl': pageUrl,
          'success': function () { },
          'cancel': function () { }
        };

        runner.weixinShare(option);
      }
    });
  };

  shareToWx();
});